<?php
    if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class Cmod_product extends MYAD_Controller{
        public $html ="";
        function __construct(){
            parent::__construct();
            $this->load->model('Cmmod_product');
            $this->load->helper('seourl');
        }
        function index(){
            $data['title'] = 'Quản lý danh mục sản phẩm';
            $data['title_table'] = 'Quản lý danh mục';
            $data['show']="cindex";
            $data['data'] = $this->Cmmod_product->listAll();
            foreach($data['data'] as $key=>$row){
                if($row['parent_id']!=0){
                    $data['data'][$key]['c_name'] = $this->get_name_category($row['parent_id']);
                }
            }                    
            $this->load->view('ad_layout',$data);
        }
        function add(){
            if(isset($_REQUEST['submit'])){
                $name = $_REQUEST['name'];
                $url = seo_url($_REQUEST['name']);
                $parent_id = $_REQUEST['parent_id'];
 		        $order= $_REQUEST['order'];
                $status = isset($_REQUEST['status'])?1:0;							
                $data = array(
                   'name' => $name ,
                   'url' => $url ,
                   'parent_id' => $parent_id ,
                   'order' => $order ,
                   'status' => $status ,				   				   
                );
                $this->Cmmod_product->insertData($data);
                $this->session->set_flashdata('message', 'Thêm danh mục thành công!');
                redirect('/quantri/cproduct');                
            }
            else{
                $data['title'] = 'Quản lý danh mục sản phẩm';
                $data['title_table'] = 'Thêm danh mục sản phẩm';
                $data['show']="cadd";
                $data['category'] = $this->get_category(0);       
                $this->load->view('ad_layout',$data);                
            }           
        }        
        
        
        
        
        
        /*
            ham nay dung de lay ten cua category 
        */
        function get_name_category($id){
            $row = $this->Cmmod_product->getNameById($id);
            if($row['name'])
                return $row['name'];
        }        
        /*
        ham lay toan bo danh muc category 4 cap
        */
        function get_category($idParent){
            $result = $this->Cmmod_product->getAllChild($idParent);
            foreach($result as $row){
                $this->html .= "<option value='".$row['id']."'>".$row['name']."</option>";
                if($this->Cmmod_product->getNumRowCat($row['id'])){
                    
                    $result1 = $this->Cmmod_product->getAllChild($row['id']);
                    foreach($result1 as $row1){
                        $this->html .= "<option value='".$row1['id']."'>----".$row1['name']."</option>";
                        if($this->Cmmod_product->getNumRowCat($row1['id'])){
                            $result2 = $this->Cmmod_product->getAllChild($row1['id']);
                            foreach($result2 as $row2){
                                $this->html .= "<option value='".$row2['id']."'>--------".$row2['name']."</option>";
                                 if($this->Cmmod_product->getNumRowCat($row2['id'])){
                                 
                                    $result3 = $this->Cmmod_product->getAllChild($row2['id']);
                                    foreach($result3 as $row3){
                                        $this->html .= "<option value='".$row3['id']."'>------------".$row3['name']."</option>";
                                    }                                 
                                 
                                 }
                            } 
                                                        
                        }
                    } 
                                       
                }
            }
            return $this->html;
        }
        /*
        ham lay toan bo danh muc category 4 cap trong do co 1 danh muc duoc chon
        */        
        function get_category_selected($idParent,$idSelected){
            $result = $this->Cmmod_product->getAllChild($idParent);
            foreach($result as $row){
                $this->html .= $idSelected;
                if($row['id']==$idSelected){
                    $this->html .= "<option value='".$row['id']."' selected='selected' >".$row['name']."</option>";
                }
                else{
                    $this->html .= "<option value='".$row['id']."' >".$row['name']."</option>";
                }
                if($this->Cmmod_product->getNumRowCat($row['id'])){
                    
                    $result1 = $this->Cmmod_product->getAllChild($row['id']);
                    foreach($result1 as $row1){
                        if($row1['id']==$idSelected){
                            $this->html .= "<option value='".$row1['id']."' selected='selected' >----".$row1['name']."</option>";
                        }
                        else{
                           $this->html .= "<option value='".$row1['id']."'>----".$row1['name']."</option>";
                        }                        
                        
                        if($this->Cmmod_product->getNumRowCat($row1['id'])){
                            $result2 = $this->Cmmod_product->getAllChild($row1['id']);
                            foreach($result2 as $row2){
                                if($row2['id']==$idSelected){
                                    $this->html .= "<option value='".$row2['id']."' selected='selected' >--------".$row2['name']."</option>";
                                }
                                else{
                                   $this->html .= "<option value='".$row2['id']."'>--------".$row2['name']."</option>";
                                }
                                 if($this->Cmmod_product->getNumRowCat($row2['id'])){
                                 
                                    $result3 = $this->Cmmod_product->getAllChild($row2['id']);
                                    foreach($result3 as $row3){
                                        if($row3['id']==$idSelected){
                                            $this->html .= "<option value='".$row3['id']."' selected='selected' >------------".$row3['name']."</option>";
                                        }
                                        else{
                                           $this->html .= "<option value='".$row3['id']."'>------------".$row3['name']."</option>";
                                        }
                                    }                                 
                                 
                                 }
                            } 
                                                        
                        }
                    } 
                                       
                }
            }
            return $this->html;
        }          
        
        
    }
?>